const { maskPhoneNumber } = require('../../../../utils/hooks');
const $api = require('../../../../request/api').API;

Page({
  data: {
    loading: false,
    rescueData: [] // 救援数据
  },
  onLoad: function (options) {
    const { orderId } = options;
    this.fetchObtainRescueProgressData(orderId);
  },

  /**
   * fetch救援进度
   * @param orderId
   */
  fetchObtainRescueProgressData(orderId) {
    this.setData({ loading: true });
    $api
      .ListFirstAidWaiter({ orderId })
      .then(res => {
        const { data } = res.data;
        this.setData({
          loading: false,
          rescueData: data
            .map(c => {
              return { ...c, encryptionPhone: maskPhoneNumber(c.phone) };
            })
            .filter(item => item.firstAidDate)
        });
      })
      .catch(() => {
        this.setData({ loading: false });
      });
  },

  /**
   * 拨打电话
   * @param event
   */
  handleMakingPhoneCalls(event) {
    const { phone } = event.currentTarget.dataset;
    if (!phone) return;
    wx.makePhoneCall({
      phoneNumber: phone,
      success: () => {
        console.log('拨打电话成功！');
      },
      fail: () => {
        console.error('拨打电话失败：', error);
      }
    });
  }
});
